<!DOCTYPE html>
<html lang="en">
<head>
<!--  问卷详情-->
    <meta charset="UTF-8">
    <title>问卷详情</title>
    <link rel="stylesheet" href="../css/bootstrap.min.css"/>
    <script src="../js/jquery.js"></script>
    <script src="../js/bootstrap.min.js"></script>
    <script src="../js/echarts.min.js"></script>
    <link href="css/anniu.css" rel="stylesheet">
    <style>
        li{
            margin-top: 8px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
<!--        标题头部-->
        <div class="row">
            <div class="col-md-2">
                <a href="wenjuan.html" class="button gray"><span class="glyphicon glyphicon-arrow-left"></span></a>
            </div>
            <div class="col-md-8">
                <h1  id="h1biaoti" style="font-weight: bolder;text-shadow:5px 5px 5px #17a2b8;text-align: center "></h1>

            </div>
            <div class="col-md-2">
                <a href="#" id="tj" style="margin-top: 6px" class="button orange">添加题目</a>
            </div>
        </div>
<!--        描述-->
        <div class="row">
            <div class="col-md-6 bg-warning col-md-offset-3">
                <h4 style="text-align: center" id="miaoshu"></h4>
            </div>
        </div>
<!--        详情-->
        <div class="row" style="font-size: 25px">
            <div class="col-md-6 col-md-offset-3">
                    <ol id="timuol">

                    </ol>
            </div>
        </div>
    </div>

<!--    添加题目-->
    <div style="font-size: 25px;"  class="modal fade" id="motak5" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h1>添加题目</h1>
                </div>
                <div class="modal-body">
                    <label>请输入要添加的题目</label>
                    <input id="tm" class="form-control"/>
                    <br>
                    <select id="timulx">
                        <option value="t" selected>填写</option>
                        <option value="x">单选</option>
                    </select>
                </div>
                <div class="modal-footer">
                    <input  id="tjtm5" type="button"  value="提交" class="btn btn-primary"/>
                    <button  class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
<!--    添加答案-->
    <div style="font-size: 25px;"  class="modal fade" id="motak6" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h1>添加答案</h1>
                </div>
                <div class="modal-body" id="tjtm">

                </div>
                <div class="modal-footer">
                    <input  id="tjtm6" type="button"  value="提交" class="btn btn-primary"/>
                    <button  class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

</body>
<script type="text/javascript">

    let us = 0;
    let ob = localStorage.getItem("obj")
    let obj =  JSON.parse(ob)

    if(obj.vote_vcondition!="正在进行"){
        $("#tj").hide()
    }
    //标题
    $("#h1biaoti").html(obj.vote_vtitled)
    $("#miaoshu").html(obj.vote_vdepict)
    //获取该标题的所有题目
    $.ajax({
        url: "xiangxixinxi",
        type: "get",
        data: {vid: obj.vote_vid},
        dataType: "json",
        success: function (data) {
            console.log(data)
            for(let obj of data){
               let divfom= document.createElement("div")
                $(divfom).attr("class","form-group has-feedback")
                $(divfom).val(obj.Subject_stype)
                $(divfom).attr("name",obj.Subject_sid)
                let label= document.createElement("label")
                label.innerHTML=obj.Subject_scontent
                $(divfom).append(label)
                if(obj.Subject_stype=="t"){
                    let tx= document.createElement("input")
                    $(tx).attr("class","form-control")
                    $(divfom).append(tx,"<br>")
                }else {
                    let xz1= document.createElement("input")
                    xz1.style.width="20px"
                    $(xz1).attr("type","radio")
                    $(xz1).attr("checked","checked")
                    $(xz1).val("赞成")
                    $(xz1).attr("name","one"+obj.Subject_sid)
                    let xz2= document.createElement("input")
                    xz2.style.width="20px"
                    $(xz2).attr("type","radio")
                    $(xz2).val("反对")
                    $(xz2).attr("name","one"+obj.Subject_sid)
                    $(divfom).append("<br>",xz1,"赞成","<br>",xz2,"反对")
                }
                $("#tjtm").append(divfom)
                let li =  document.createElement("li")
                li.innerHTML=obj.Subject_scontent
                            //删除题目
                            let bun = document.createElement("button")
                            $(bun).attr("class","glyphicon glyphicon-trash")
                            if(us==1){
                                $(bun).hide()
                            }
                            bun.style.float="right"
                            bun.style.color="red"
                            $(bun).on("click",function(){
                                $.ajax({
                                    url:"delecttimu",
                                    type:"post",
                                    data:{sid:obj.Subject_sid},
                                    dataType:"json",
                                    success:function(data){
                                        if(data){
                                            alert("删除题目成功")
                                            window.location.reload();
                                        }else {
                                            alert("删除题目失败")
                                        }
                                    }
                                });
                            });
                $(li).append(bun)
                $("#timuol").append(li)
                //添加答案
                //ture  加载填写答案   false 加载选项
                let ol = document.createElement("ol")
                $(li).append(ol)
                if(obj.Subject_stype=="t"){
                    $.ajax({
                        url: "getdaan",
                        type: "get",
                        data: {vid: obj.vote_vid, sid: obj.Subject_sid},
                        dataType: "json",
                        success: function (data) {
                            for (let o of data) {
                                let dali = document.createElement("li")
                                dali.innerHTML = o.Hname+"答："+ o.Answer_acontent
                                //填写答案删除
                                let bu = document.createElement("button")
                                if(us==1){
                                    $(bu).hide()
                                }
                                $(bu).attr("name","yincang")
                                $(bu).attr("class","glyphicon glyphicon-trash")
                                    bu.style.float="right"
                                    bu.style.color="blue"
                                    $(bu).on("click",function(){
                                        //删除答案
                                        $.ajax({
                                            url:"delectdaan",
                                            type:"post",
                                            data:{daid:o.Answer_aid},
                                            dataType:"json",
                                            success:function(data){
                                                if(data){
                                                    alert("删除答案成功")
                                                    window.location.reload();
                                                }else {
                                                    alert("删除答案失败")
                                                }
                                            }
                                        });
                                    });
                                $(dali).append(bu)
                                $(ol).append(dali)
                            }
                        }
                    })
                }else {
                    $.ajax({
                        url: "getdaan1",
                        type: "get",
                        data: {vid: obj.vote_vid, sid: obj.Subject_sid},
                        dataType: "json",
                        success: function (data) {
                            for (let o of data) {
                                let dali = document.createElement("li")
                                if(o.BAnswer_boolean=="t"){
                                    dali.innerHTML=o.Hname+"：赞成！"
                                    dali.style.color="green"
                                }else {
                                    dali.innerHTML=o.Hname+"：反对！"
                                    dali.style.color="red"
                                }
                                let bu = document.createElement("button")
                                if(us==1){
                                    $(bu).hide()
                                }
                                $(bu).attr("name","yincang")
                                $(bu).attr("class","glyphicon glyphicon-trash")
                                bu.style.float="right"
                                bu.style.color="blue"
                                $(bu).on("click",function(){
                                    //删除答案
                                    $.ajax({
                                        url:"delectdaan1",
                                        type:"post",
                                        data:{daid:o.BAnswer_aid},
                                        dataType:"json",
                                        success:function(data){
                                            if(data){
                                                alert("删除答案成功")
                                                window.location.reload();
                                            }else {
                                                alert("删除答案失败")
                                            }
                                        }
                                    });
                                });
                                $(dali).append(bu)
                                $(ol).append(dali)
                            }
                        }
                    })
                }

            }
        }
    })

    /**
     * 判断是否是住户
     */
    $.ajax({
        url:"iszhuhu",
        type:"get",
        dataType:"json",
        success:function(data){
            if(data){
                $("#tj").html("添加答案")
                us=1
            }
        }
    });

    /**
     * 添加题目或答案弹出对应的模态框
     */
    $("#tj").on("click",function(){
        let str =  $("#tj").html()
        if(str=="添加题目"){
            $("#motak5").modal("show")
        }else {
            $("#motak6").modal("show")
        }
    });
    /**
     * 添加题目
     */
    $("#tjtm5").on("click",function (){
        let timu = $("#tm").val()
        let lx = $("#timulx").val()
        if(timu==""){
            alert("题目不能为空")
        }
        $.ajax({
            url:"addtimu",
            type:"post",
            dataType:"json",
            data:{timu:timu,vid:obj.vote_vid,lx:lx},
            success:function(data){
                if(data){
                    alert("添加成功")
                    //  $("#motak").on("shown.bs.modal", function () {}).modal('show')
                    window.location.reload()
                }else {
                    alert("添加失败,题目名已存在")
                }
            }
        });
    })

    /**
     * 添加答案
     */
    $("#tjtm6").on("click",function (){
        let divs = $("#tjtm").get(0).children
        let arr = new Array(divs.length)
        for(let i = 0;i<divs.length;i++){
            let div=divs[i]
            let divvalue = $(div).val()
            let daan ;
            let sid =  $(div).attr("name")
            let fs;
            if(divvalue=="t"){
               let v= div.children[1].value
                if(v==""){
                    alert("数据必须填完")
                     return;
                }
                daan=v
                fs="t";
            }else {
              let xz =div.children[2]
                if(xz.checked){
                    daan="t"
                }else {
                    daan="f"
                }
                fs="x"
            }
            arr[i]={sid,daan,fs}
        }
        $.ajax({
            url:"adddaan",
            type:"post",
            dataType:"json",
            data:{ "str":JSON.stringify(arr)},
            success:function (data){
                console.log(data)
                if(data){
                    alert("添加成功")
                    window.location.reload()
                }else {
                    alert("添加失败")
                }
            }
        })

    })


</script>
</html>